package com.neusoft.yiyang.care.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neusoft.yiyang.common.entity.care.CareRecord;
import com.neusoft.yiyang.care.service.CareRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

@Tag(name = "护理记录管理接口", description = "负责护理记录的查询、更新、删除等操作，记录护理执行情况")
@RestController
@RequestMapping("/care-records")
public class CareRecordController {

    @Autowired
    private CareRecordService careRecordService;

    @Operation(summary = "根据ID查询护理记录", description = "通过护理记录ID获取详细的护理执行信息")
    @GetMapping("/{careRecordId}")
    public CareRecord getCareRecordById(
            @Parameter(description = "护理记录ID", required = true) @PathVariable Integer careRecordId) {
        return careRecordService.getCareRecordById(careRecordId);
    }

    @Operation(summary = "查询指定客户的护理记录", description = "获取某一客户的所有护理执行历史记录")
    @GetMapping("/by-customer/{customerId}")
    public List<CareRecord> getCareRecordsByCustomerId(
            @Parameter(description = "客户ID", required = true) @PathVariable Integer customerId) {
        return careRecordService.getCareRecordsByCustomerId(customerId);
    }

    @Operation(summary = "查询指定护工的护理记录", description = "获取某一护工执行的所有护理记录")
    @GetMapping("/by-caretaker")
    public List<CareRecord> getCareRecordsByCaretakerId(
            @Parameter(description = "护工ID", required = true) @RequestParam("caretakerId") Integer caretakerId) {
        return careRecordService.getCareRecordsByCaretakerId(caretakerId);
    }

    @Operation(summary = "查询所有护理记录", description = "获取系统中所有的护理执行记录（谨慎使用，数据量大时建议分页）")
    @GetMapping("/all")
    public List<CareRecord> getAllCareRecords() {
        return careRecordService.getAllCareRecords();
    }

    @Operation(summary = "更新护理记录", description = "修改护理记录的详情，支持更新护理项目、时间、反馈、照片等信息")
    @PutMapping("/{careRecordId}")
    public void updateCareRecord(
            @Parameter(description = "护理记录ID", required = true) @PathVariable Integer careRecordId,
            @Parameter(description = "护理项目ID（可选）") @RequestParam(required = false) Integer careItemId,
            @Parameter(description = "护工ID（可选）") @RequestParam(required = false) Integer caretakerId,
            @Parameter(description = "护理时间（可选，格式：yyyy-MM-dd HH:mm:ss）")
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date careTime,
            @Parameter(description = "执行次数（可选）") @RequestParam(required = false) Integer quantity,
            @Parameter(description = "老人反馈（可选）") @RequestParam(required = false) String feedback,
            @Parameter(description = "执行状态（可选，normal=已完成/abnormal=未完成/missed=漏执行）")
            @RequestParam(required = false) String status,
            @Parameter(description = "护理照片（可选，支持PNG/JPG格式，不超过5MB）")
            @RequestParam(required = false) MultipartFile file) {
        careRecordService.updateCareRecord(
                careRecordId, careItemId, caretakerId, careTime,
                quantity, feedback, status, file
        );
    }

    @Operation(summary = "删除护理记录", description = "根据ID删除指定的护理记录")
    @DeleteMapping("/{careRecordId}")
    public void deleteCareRecord(
            @Parameter(description = "护理记录ID", required = true) @PathVariable Integer careRecordId) {
        careRecordService.deleteCareRecord(careRecordId);
    }

    @Operation(summary = "分页查询护理记录", description = "多条件筛选护理记录并分页返回，支持按客户、护工、时间等筛选")
    @GetMapping("/page")
    public Page<CareRecord> pageQuery(
            @Parameter(description = "当前页码，默认1") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页条数，默认10") @RequestParam(defaultValue = "10") Integer pageSize,
            @Parameter(description = "客户姓名（可选，支持模糊匹配）") @RequestParam(required = false) String customerName,
            @Parameter(description = "护工姓名（可选，支持模糊匹配）") @RequestParam(required = false) String caretakerName,
            @Parameter(description = "开始日期（可选，格式：yyyy-MM-dd HH:mm:ss）")
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startDate,
            @Parameter(description = "结束日期（可选，格式：yyyy-MM-dd HH:mm:ss）")
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endDate,
            @Parameter(description = "护理项目名称（可选，支持模糊匹配）") @RequestParam(required = false) String careItemName,
            @Parameter(description = "护工ID（可选，精确匹配）") @RequestParam(required = false) Integer caretakerId,
            @Parameter(description = "执行状态（可选，normal/abnormal/missed）") @RequestParam(required = false) String status
    ) {
        Page<CareRecord> page = new Page<>(pageNum, pageSize);
        return careRecordService.pageQuery(page, customerName, caretakerName,
                startDate, endDate, careItemName,
                caretakerId, status);
    }
}